<?php
require_once './class/plugin/control/interface.PluginControl.php';
require_once './class/menu/control/interface.MenuControl.php';

include_once './class/timex/control/class.Timex.php';

require_once './class/box/view/class.BoxStandard.php';
require_once './class/box/view/class.BoxApanel.php';

require_once './class/pager/control/class.Pager.php';

Class Box implements PluginControl, MenuControl {

	protected $objData = NULL;
	protected $objView = NULL;
	protected $objSession = NULL;
  	protected $pagerUrl = NULL;
 	protected $menuActionVarName = 'boxA';
	protected $pagerLimit = 10;
	protected $repeatableModule = false;
  	public $rootFileName = NULL;
    public $itemVarName = 'boxI';
	public $theBoxUrl = NULL;
	public $addBoxUrl = NULL;
	public $delBoxUrl = NULL;
	public $modBoxUrl = NULL;
	public $subjectId = 0;

	function __construct($objData) {
		$this->objData = $objData;

		$this->objView = new BoxStandard();
		$this->objData->install();
	}
	
	public function isRepeatableModule() {
		return $this->repeatableModule;
	}	
	
	public function getModuleName() {
		return $this->objView->getModuleName();
	}

	public function getModuleDescription() {
		return $this->objView->getModuleDescription();
	}
	
// *** OBJECT PROFILE ***
	public function setObjProfile($objProfile) {
		$this->objProfile = $objProfile;
	}

	protected function getTheProfile($profileId) {
	        $html = NULL;
	        if(is_object($this->objProfile)) {
			$html = $this->objProfile->getTheProfile($profileId);
	        }

		return $html;
	}
// *** END OF OBJECT PROFILE ***	
	
	public function setSubjectId($subjectId) {
		$this->subjectId = $this->checkNumber($subjectId);
	}

	public function setPagerLimit($pagerLimit) {
		$this->pagerLimit = $pagerLimit;
	}

	public function setPagerUrl($pagerUrl) {
		$this->pagerUrl = $pagerUrl;
	}

	public function setItemVarName($itemVarName) {
		$this->itemVarName = $itemVarName;
	}

	public function setTheBoxUrl($theBoxUrl) {
		$this->theBoxUrl = $theBoxUrl;
	}

	public function setAddBoxUrl($addBoxUrl) {
		$this->addBoxUrl = $addBoxUrl;
	}

	public function setDelBoxUrl($delBoxUrl) {
		$this->delBoxUrl = $delBoxUrl;
	}

	public function setModBoxUrl($modBoxUrl) {
		$this->modBoxUrl = $modBoxUrl;
	}

	public function setMenuActionVarName($menuActionVarName) {
		$this->menuActionVarName = $menuActionVarName;
	}

	public function setObjSession($objSession) {
		$this->objSession = $objSession;
	}
	
	public function setObjView($objView) {
		$this->objView = $objView;
	}
	
	protected function cleanInputData($inputData) {
		return addslashes($inputData);
	}

	protected function cleanOutputData($outputData) {
		return stripslashes($outputData);
	}

	private function checkNumber($theVar) {
		if (preg_match("/^[\-]?[0-9]+$/",$theVar)) {
			return (int)$theVar;
		} else {
			return 0;
		}
	}

	public function getMenuActionVarName() {
		if(isset($_GET[$this->menuActionVarName])) {
			$menuValue = $_GET[$this->menuActionVarName];
		} else {
			$menuValue = NULL;
		}

		return $menuValue;
	}

	public function getCurrentBoxId() {
	        if (isset($_GET[$this->itemVarName])) {
			return $this->checkNumber($_GET[$this->itemVarName]);
		} else {
			return 0;
		}
	}
	
	public function getCurrentActiveBoxId() {
	        if ($this->objData->isTheBoxActiveById($this->getCurrentBoxId())) {
			return $this->getCurrentBoxId();
		} else {
			return 0;
		}
	}

	public function setRootFileName($rootFileName) {
		$this->rootFileName = $rootFileName;

		$this->setTheBoxUrl($this->rootFileName.'&amp;'.$this->menuActionVarName.'=theBox');
		$this->setAddBoxUrl($this->rootFileName.'&amp;'.$this->menuActionVarName.'=addBox');
		$this->setDelBoxUrl($this->rootFileName.'&amp;'.$this->menuActionVarName.'=delBox');
		$this->setModBoxUrl($this->rootFileName.'&amp;'.$this->menuActionVarName.'=modBox');
	}

	protected function getTime($timeStamp) {
		$time = new Timex($timeStamp);
		return $time->getTime();
	}

	protected function getPager($total) {
		$pager = new Pager($total);
		$pager->setNbrOfItemsOnPage($this->pagerLimit);
		$pager->setBaseUrl($this->pagerUrl);
		$pager->setOffsetGetVar('offset'.$this->itemVarName);

		$pager->setObjView(new PagerStandard());
		$pagerData = $pager->getPagerElements();

		$pager->setObjView(new PagerApanel());
		$pagerData['apanel'] = $pager->getPagerElements();

		return $pagerData;
	}

	public function getTheBoxInformation($boxRow) {
		$boxInfo['id'] = $boxRow['id_box'];
		$boxInfo['id_subject'] = $boxRow['id_subject'];
		$boxInfo['name'] = $boxRow['name'];
		$boxInfo['desc'] = $boxRow['description'];
		$boxInfo['time'] = $this->getTime($boxRow['tstamp']);
		$boxInfo['state'] = $boxRow['state'];
		$boxInfo['sort'] = $boxRow['sort'];

		$boxInfo['url']['all'] = $this->rootFileName;
		$boxInfo['url']['the'] = $this->theBoxUrl.'&amp;'.$this->itemVarName.'='.$boxRow['id_box'];
		$boxInfo['url']['add'] = $this->addBoxUrl;
		$boxInfo['url']['del'] = $this->delBoxUrl.'&amp;'.$this->itemVarName.'='.$boxRow['id_box'];
		$boxInfo['url']['mod'] = $this->modBoxUrl.'&amp;'.$this->itemVarName.'='.$boxRow['id_box'];

		return $boxInfo;
	}

	public function getBoxsInformation($boxRows) {
		$boxItems = array();
		foreach ($boxRows as $boxRow) {
			$boxItems[] = $this->getTheBoxInformation($boxRow);
		}

		return $boxItems;
	}
	
	public function subjectActiveBoxsFreeAccess() {
		$html = NULL;
		if ($this->subjectId) {
			$totalBoxs = $this->objData->howManySubjectActiveBoxs($this->subjectId);
			$pager = $this->getPager($totalBoxs);
			$boxRows = $this->objData->getLimitedSubjectActiveBoxs($this->subjectId,$this->pagerLimit,$pager['currentoffset']);

			$boxData['url']['all'] = $this->rootFileName;
			$boxData['url']['add'] = $this->addBoxUrl;
			if ($boxRows) {
				$boxData['records']['total'] = $totalBoxs;
				$boxData['records']['page'] = $this->pagerLimit;
				$boxData['boxs'] = $this->getBoxsInformation($boxRows);
				$boxData['pager'] = $pager;
				$html .= $this->objView->subjectBoxs($boxData);
			} else {
				$html .= $this->objView->noSubjectBoxs($boxData);
			}
		}
		return $html;
	}

	public function subjectBoxsFreeAccess() {
	        $html = NULL;
	        if ($this->subjectId) {
		        $totalBoxs = $this->objData->howManySubjectBoxs($this->subjectId);
		        $pager = $this->getPager($totalBoxs);
	        	$boxRows = $this->objData->getLimitedSubjectBoxs($this->subjectId,$this->pagerLimit,$pager['currentoffset']);

		        $boxData['url']['all'] = $this->rootFileName;
		        $boxData['url']['add'] = $this->addBoxUrl;
			if ($boxRows) {
		        	$boxData['records']['total'] = $totalBoxs;
		        	$boxData['records']['page'] = $this->pagerLimit;
			        $boxData['boxs'] = $this->getBoxsInformation($boxRows);
			        $boxData['pager'] = $pager;
				$html .= $this->objView->subjectBoxs($boxData);
			} else {
				$html .= $this->objView->noSubjectBoxs($boxData);
			}
		}
		return $html;
	}

	public function subjectBoxs() {
	        if (is_object($this->objSession)) {
		        if ($this->objSession->isSessionSet()) {
				return $this->subjectBoxsFreeAccess();
	        	}
		}
	}
	
	public function subjectBoxsOrderByName() {
	        $html = NULL;
	        if ($this->subjectId) {
		        $totalBoxs = $this->objData->howManySubjectBoxs($this->subjectId);
		        $pager = $this->getPager($totalBoxs);
	        	$boxRows = $this->objData->getLimitedSubjectBoxsOrderByName($this->subjectId,$this->pagerLimit,$pager['currentoffset']);

		        $boxData['url']['all'] = $this->rootFileName;
		        $boxData['url']['add'] = $this->addBoxUrl;
			if ($boxRows) {
		        	$boxData['records']['total'] = $totalBoxs;
		        	$boxData['records']['page'] = $this->pagerLimit;
			        $boxData['boxs'] = $this->getBoxsInformation($boxRows);
			        $boxData['pager'] = $pager;
				$html .= $this->objView->subjectBoxs($boxData);
			} else {
				$html .= $this->objView->noSubjectBoxs($boxData);
			}
		}
		return $html;
	}	

	public function theBoxFreeAccess($boxId) {
		$boxRow = $this->objData->getTheBoxById($boxId);

		$html = NULL;
		if($boxRow !== false) {
			$html .= $this->objView->theBox($this->getTheBoxInformation($boxRow));
		}

		return $html;
	}

	public function theBox($boxId) {
	        if (is_object($this->objSession)) {
		        if ($this->objSession->isSessionSet()) {
				return $this->theBoxFreeAccess($boxId);
	        	}
		}
	}

	public function addBox() {
	        $html = NULL;
	        if (is_object($this->objSession)) {
		        if ($this->objSession->isSessionSet()) {
		                $boxData['url']['all'] = $this->rootFileName;
				$html .= $this->objView->addBox($boxData);
	        	}
		}
		return $html;
	}

	public function addBoxSave() {
	        $result = 0;
		        if ($this->getPostName() && $this->subjectId && !$this->getPostModBoxId()) {
				$boxData['id_subject'] = $this->subjectId;
				$boxData['name'] = $this->getPostName();
				$boxData['desc'] = ($this->getPostDesc()) ? $this->getPostDesc() : NULL;
				$boxData['tstamp'] = mktime();
				$boxData['state'] = 1;
				        $sort = $this->objData->howManySubjectBoxs($this->subjectId);
				$boxData['sort'] = $sort + 1;

				$result = $this->objData->insertDataBox($boxData);

				if ($result) {
					if ($boxRow = $this->objData->getTheBoxByTime($boxData['tstamp'])) {
                                       		$result = $boxRow['id_box'];
					}
				}
			}
		return $result;
	}

	public function delTheBox($boxId) {
		$boxRow = $this->objData->getTheBoxById($boxId);

		$html = NULL;
		if($boxRow !== false) {
			$html .= $this->objView->delBox($this->getTheBoxInformation($boxRow));
		}

		return $html;
	}

	public function delTheBoxSave($boxId) {
	        $result = false;
	        if ($boxId) {
			$result = $this->objData->delTheBoxById($boxId);
			$this->sortBoxs();
		}
		return $result;
	}


	public function statesSave() {
		if ($this->getPostStateBoxIds() && $this->subjectId) {
			$boxIds = $this->getPostStateBoxIds();
			$totalBoxs = $this->objData->howManySubjectBoxs($this->subjectId);
			$pager = $this->getPager($totalBoxs);
			$boxRows = $this->objData->getLimitedSubjectBoxs($this->subjectId,$this->pagerLimit,$pager['currentoffset']);
			foreach ($boxRows as $boxRow) {
				$this->objData->setState(0,$boxRow['id_box']);
			}
			foreach ($boxIds as $boxId) {
				$this->objData->setState(1,$boxId);
			}
		}
	}

	public function sortsSave() {
		if ($this->getPostSortBoxIds()) {
		        $orders = $this->getPostSortBoxIds();
		        foreach ($orders as $key=>$order) {
				$this->objData->setSort($order,$key);
			}
		}
	}

	protected function sortBoxs() {
		$boxRows = $this->objData->getSubjectBoxs($this->subjectId);

		if ($boxRows) {
			foreach($boxRows as $key=>$boxRow) {
				$this->objData->setSort($key+1,$boxRow['id_box']);
			}
		}
	}

	public function delSubjectBoxs() {
		if ($boxRows = $this->objData->getsubjectBoxs($this->subjectId)) {
			foreach($boxRows as $boxRow) {
				$this->delTheBoxSave($boxRow['id_box']);
			}
		}
	}
	
	public function modTheBoxSave() {
                if ($boxId = $this->getPostModBoxId()) {
		        if ($this->getPostName()) {
				$this->objData->setName($this->getPostName(),$boxId);
			}
			
			if ($this->getPostDesc()) {
				$this->objData->setDesc($this->getPostDesc(),$boxId);
			}
		}
	}

	public function modTheBox($boxId) {
	        $html = NULL;
	        if (is_object($this->objSession)) {
	  		if ($this->objSession->isSessionSet()) {
	  		        if ($boxRow = $this->objData->getTheBoxById($boxId)) {
					$html .= $this->objView->modTheBoxForm($this->getTheBoxInformation($boxRow));
				}
	  		}
		}
		return $html;
	}

	public function getPostDelBoxId() {
		if (isset($_POST['boxid_del'])) {
		        return $this->checkNumber($_POST['boxid_del']);
		} else {
			return false;
		}
	}
	
	public function getPostModBoxId() {
		if (isset($_POST['boxid_mod'])) {
		        return $this->checkNumber($_POST['boxid_mod']);
		} else {
			return false;
		}
	}

	public function getPostStateBoxIds() {
	        if (isset($_POST['boxids_state'])) {
	                return $_POST['boxids_state'];
		} else {
			return false;
		}
	}

	public function getPostSortBoxIds() {
		if(isset($_POST['boxids_sort'])) {
			return $_POST['boxids_sort'];
		} else {
			return false;
		}
	}
	
	public function getPostName() {
		if (isset($_POST['box_name'])) {
			return $this->cleanInputData($_POST['box_name']);
		} else {
			return false;
		}
	}
	
	public function getPostDesc() {
		if (isset($_POST['box_desc'])) {
			return $this->cleanInputData($_POST['box_desc']);
		} else {
			return false;
		}
	}

	public function boxsApanel() {

		$html = NULL;
		if (is_object($this->objSession)) {
		        if ($this->objSession->isSessionSet()) {
	        		switch($this->getMenuActionVarName()) {
					case 'theBox':
						$html .= $this->theBox($this->getCurrentBoxId());
						break;
					case 'addBox':
						$html .= $this->addBox();
					        break;
					case 'delBox':
						$html .= $this->delTheBox($this->getCurrentBoxId());
					        break;
					case 'modBox':
						$html .= $this->modTheBox($this->getCurrentBoxId());
					        break;
					default:
				        $this->modTheBoxSave();
				        $this->addBoxSave();
				        $this->sortsSave();
						$this->statesSave();
				        $this->delTheBoxSave($this->getPostDelBoxId());
				        $this->setPagerUrl($this->rootFileName);
						$html .= $this->subjectBoxs();
					        break;
				}
			}
		}
		return $html;
	}
	
	public function boxsWebsite() {

		$html = NULL;
       		switch($this->getMenuActionVarName()) {
			case 'theBox':
				$html .= $this->theBoxFreeAccess($this->getCurrentActiveBoxId());
				break;
			default:
				$this->setPagerUrl($this->rootFileName);
				$html .= $this->subjectActiveBoxsFreeAccess();
				break;
		}
		return $html;
	}
	
	public function pluginModuleApanel($subjectId,$rootFileName,$assignments) {
		$this->setSubjectId($subjectId);
		$this->setRootFileName($rootFileName);

		$pluginData['body'] = $this->boxsApanel();
		$pluginData['tags']['js'] = $this->objView->getTagsJs();
		$pluginData['tags']['css'] = $this->objView->getTagsCss();

		return $pluginData['body'];			
	}
	
	public function pluginModule($subjectId,$rootFileName,$assignments) {
		$this->setSubjectId($subjectId);
		$this->setRootFileName($rootFileName);

		$pluginData['body'] = $this->boxsWebsite();
		$pluginData['tags']['js'] = $this->objView->getTagsJs();
		$pluginData['tags']['css'] = $this->objView->getTagsCss();

		return $pluginData;		
	}
	
	public function pluginModuleRemove($subjectId) {
	        $this->setSubjectId($subjectId);
		$this->delSubjectBoxs();
	}
	
	public function getActiveBoxs() {
		return $this->objData->getActiveBoxs();
	}
	
	public function getActiveBoxsOrderByName() {
		return $this->objData->getActiveBoxsOrderByName();
	}	
	
	public function getSubjectBoxs($subjectId) {
		return $this->objData->getSubjectActiveBoxs($subjectId);
	}
	
	public function getTheBox($boxId) {
		return $this->getTheBoxInformation($this->objData->getTheBoxById($boxId));
	}
}

?>
